#include "gofgame/dungeonkeeper/game/ResultLayer.h"
#include "gofgame/dungeonkeeper/MapScene/MapScene.h"
#include "gofgame/dungeonkeeper/loadingscene/LoadingScene.h"

using namespace gofgame::dungeonkeeper::game;
using namespace gofgame::dungeonkeeper::mapscene;
using namespace gofgame::dungeonkeeper::loadingscene;

bool ResultLayer::init(){
	bool bRet = false;
	do 
	{
		CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(0,0,0,180)));
		this->setVisible(false);
		bRet = true;
	} while (0);
	return bRet;
}

void ResultLayer::setResult(bool success,int rank,int nextLevel){
	CCString *str;
	if(success){
		str = CCString::create("gong xi ni tong guan~");
		if (rank == 1)
		{
			star1 = CCSprite::create("ResultLayer/star_l.png");
			star2 = CCSprite::create("ResultLayer/star_bm.png");
			star3 = CCSprite::create("ResultLayer/star_br.png");
		} 
		else if (rank == 2)
		{
			star1 = CCSprite::create("ResultLayer/star_l.png");
			star2 = CCSprite::create("ResultLayer/star_m.png");
			star3 = CCSprite::create("ResultLayer/star_br.png");
		} 
		else if (rank == 3)
		{
			star1 = CCSprite::create("testUnit.jpg");
			star2 = CCSprite::create("ResultLayer/star_m.png");
			star3 = CCSprite::create("ResultLayer/star_r.png");
		}
		star1->setPosition(ccp(480,400));
		star1->setScale(0.7f);
		star2->setPosition(ccp(600,400));
		star2->setScale(0.7f);
		star3->setPosition(ccp(720,400));
		star3->setScale(0.7f);
		this->addChild(star1);
		this->addChild(star2);
		this->addChild(star3);
	} else {
		str = CCString::create("dui bu qi ,ni shi bai le ");
	}
	//label
	label = CCLabelTTF::create(str->getCString(),"",25);
	label->setPosition(ccp(600,600));
	this->addChild(label);
	//menu 
	CCMenu *menu = CCMenu::create();
	CCMenuItemImage *main = CCMenuItemImage::create("ResultLayer/menu.png","ResultLayer/menu.png",this,menu_selector(ResultLayer::onClickMainMenu));
	CCMenuItemImage *reStrat = CCMenuItemImage::create("ResultLayer/restart.png","ResultLayer/restart.png",this,menu_selector(ResultLayer::onClickRestart));
	CCMenuItemImage *next = CCMenuItemImage::create("ResultLayer/next.png","ResultLayer/next.png",this,menu_selector(ResultLayer::onClickRefresh));
	menu->addChild(main);
	menu->addChild(reStrat);
	if(success){
		menu->addChild(next);
	}
	menu->alignItemsHorizontallyWithPadding(20);
	menu->setPosition(ccp(600,200));
	this->addChild(menu);
	this->setVisible(true);
}

void ResultLayer::onClickMainMenu(CCObject *psender){
	MapScene *mp = MapScene::create();
	CCDirector::sharedDirector()->replaceScene(mp);
	this->removeFromParentAndCleanup(true);
};

void ResultLayer::onClickRestart(CCObject *psender){
	LoadingScene *loadingscene = LoadingScene::create();
	CCDirector::sharedDirector()->replaceScene(loadingscene);
	this->removeFromParentAndCleanup(true);
};

void ResultLayer::onClickRefresh(CCObject *psender){

};